/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hello;

import java.io.*;
import java.lang.*;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

/**
 * @author Joao Pinto
 */
public class MarketGest extends MIDlet implements CommandListener {

    private boolean midletPaused = false;
    private boolean isOnline = false;
    private RecordStore recordStore = null;
    private int recordID = 0;
    private MyCanvas grafico;
    private double[] valores;
    private String[] datas;
    private String empresaGlobal;
    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command exitCommand;
    private Command itemCommand;
    private Command itemCommand1;
    private Command screenCommand;
    private Command screenCommand1;
    private Command itemHistorico1;
    private Command itemLigar;
    private Command itemDesligar;
    private Command itemHistorico;
    private Command itemConsultar;
    private Command screenCommand2;
    private Command mostrarCommand2;
    private Command mostrarCommand;
    private Command addTickCommand;
    private Command exitCommand1;
    private Command okCommand;
    private Command exitCommand2;
    private Command okCommand1;
    private Command exitCommand3;
    private Form formOffline;
    private StringItem stringItem;
    private Form formOnline;
    private StringItem stringItem1;
    private Form formConsultar;
    private ChoiceGroup choiceGroupEmpresas;
    private Form formHistorico;
    private ChoiceGroup choiceGroupEmpresas2;
    private Form novoTick;
    private TextField textnewTickName;
    private TextBox textBox;
    private Form novoTick2;
    private TextField textNewTick2;
    //</editor-fold>//GEN-END:|fields|0|
    private Command addTickCommand2;
    private Command backCom;
    private Command exitCommand4;

    /**
     * The MarketGest constructor.
     */
    public MarketGest() {
    }

    /**
     * Funcao recebe uma string e retorna um array bidimensional com a cotaÃƒÂ§ÃƒÂ£o dos ultimos 7 dias
     */
    String[][] parseHistorico(String s, int numDias) {

        String[][] sArray = new String[numDias][2];

        s = s.substring(s.indexOf("\n") + 1);

        for (int i = 0; i < numDias; i++) {
            sArray[i][0] = s.substring(0, s.indexOf(","));
            s = s.substring(s.indexOf(",") + 1);
            s = s.substring(s.indexOf(",") + 1);
            s = s.substring(s.indexOf(",") + 1);
            s = s.substring(s.indexOf(",") + 1);
            sArray[i][1] = s.substring(0, s.indexOf(","));
            s = s.substring(s.indexOf("\n") + 1);
        }

        return sArray;
    }

    /**
     * Funcao recebe uma string e retorna um array com a cotaÃƒÂ§ÃƒÂ£o actual
     */
    String[] parseCotacaoHora(String s) {
        String newString = new String();
        String[] infoStrings = new String[4];

        //Retira todos os " da string
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != '"') {
                newString = newString.concat(String.valueOf(s.charAt(i)));
            }
        }

        int coma = newString.indexOf(",");
        infoStrings[0] = newString.substring(0, coma);
        newString = newString.substring(coma + 1);

        coma = newString.indexOf(",");
        infoStrings[1] = newString.substring(0, coma);
        newString = newString.substring(coma + 1);

        coma = newString.indexOf(",");
        infoStrings[2] = newString.substring(0, coma);
        newString = newString.substring(coma + 1);

        coma = newString.indexOf(",");
        infoStrings[3] = newString.substring(0, coma);

        return infoStrings;
    }

    /**
     * Funcao que abre uma ligacao HTTP e recebe as cotacoes da bolsa, retorna a informaÃƒÂ§ÃƒÂ£o lida numa String
     */
    public String getHttpData(String url) throws IOException {

        HttpConnection httpConn = null;

        InputStream is = null;
        OutputStream os = null;
        StringBuffer sb = null;

        try {
            // Open an HTTP Connection object
            httpConn = (HttpConnection) Connector.open(url);

            // Setup HTTP Request
            httpConn.setRequestMethod(HttpConnection.GET);
            httpConn.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Confirguration/CLDC-1.0");

            // This function retrieves the information of this connection
            //getConnectionInformation(httpConn);

            /** Initiate connection and check for the response code. If the
            response code is HTTP_OK then get the content from the target
             **/
            int respCode = httpConn.getResponseCode();

            /*Se pÃƒÂ¡gina movida entÃƒÂ£o abre-se ligaÃƒÂ§ÃƒÂ£o para novo URL*/
            if (respCode == httpConn.HTTP_MOVED_PERM) {
                String newUrl = httpConn.getHeaderField("Location");
                httpConn.close();

                // Open an HTTP Connection object
                httpConn = (HttpConnection) Connector.open(newUrl);

                // Setup HTTP Request
                httpConn.setRequestMethod(HttpConnection.GET);
                httpConn.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Confirguration/CLDC-1.0");
                respCode = httpConn.getResponseCode();
            }

            if (respCode == httpConn.HTTP_OK) {
                sb = new StringBuffer();
                os = httpConn.openOutputStream();
                is = httpConn.openDataInputStream();
                int chr;
                while ((chr = is.read()) != -1) {
                    sb.append((char) chr);
                }

            } else {
                return null;
            }
        } finally {
            if (is != null) {
                is.close();
            }
            if (os != null) {
                os.close();
            }
            if (httpConn != null) {
                httpConn.close();
            }
        }



        return sb.toString();
    }

    /**
     * Apaga registos de uma empresa no recordStore
     */
    private void apagaRegistos(String tick) {
        String record = null;

        try {

            RecordStore rs = RecordStore.openRecordStore("historico", true);

            for (int i = 0; i < rs.getNumRecords(); i++) {
                record = new String(rs.getRecord(i));
                record = record.substring(0, record.indexOf("|"));
                if (tick.compareTo(record) == 0) {
                    rs.deleteRecord(i);
                }
            }

        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }

    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
    // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
    // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getFormOffline());//GEN-LINE:|3-startMIDlet|1|3-postAction
    // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
    // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
    // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
    // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        String tick;
        
        try{
            RecordStore.deleteRecordStore("historico");
        }
        catch (RecordStoreException ex)
        {
            ex.printStackTrace();
        }
        
        // write pre-action user code here
        if (displayable == formConsultar) {//GEN-BEGIN:|7-commandAction|1|103-preAction
            if (command == addTickCommand) {//GEN-END:|7-commandAction|1|103-preAction
                // write pre-action user code here
                switchDisplayable(null, getNovoTick());//GEN-LINE:|7-commandAction|2|103-postAction
            // write post-action user code here
            } else if (command == exitCommand1) {//GEN-LINE:|7-commandAction|3|100-preAction
                // write pre-action user code here
                switchDisplayable(null, getFormOnline());//GEN-LINE:|7-commandAction|4|100-postAction
            // write post-action user code here
            } else if (command == mostrarCommand2) {//GEN-LINE:|7-commandAction|5|71-preAction
                try {

                    //selecciona o tick name

                    tick = new String(choiceGroupEmpresas.getString(choiceGroupEmpresas.getSelectedIndex()));

               
                    //verifica se o tick name é valido        
                    if(getHttpData("http://ichart.finance.yahoo.com/table.csv?a=10&b=5&c=2007&d=10&e=19&f=2007&g=d&ignore=.csv&s="+tick)==null)
                    {
                        Alert alert = new Alert("Erro", "Tick name desconhecido", null, AlertType.ERROR);
                        alert.setTimeout(3000);
                        Display display = Display.getDisplay(this);
                        display.setCurrent(alert, formConsultar);
                    }
                    else {
                        //ligacao HTTP para retorno da cotacao actual
                        String data = getHttpData("http://finance.yahoo.com/d/quotes.csv?f=sl1d1t1c1ohgv&e=.csv&s=" + tick);

                        String[] info = new String[3];
                        info = parseCotacaoHora(data);

                        //apresenta cotacao numa caixa de alert
                        Alert alert = new Alert("Cotacao " + info[0], "Valor: " + info[1] + "\n" + info[3] + "\n" + info[2], null, AlertType.INFO);
                        alert.setTimeout(10000);
                        Display display = Display.getDisplay(this);
                        display.setCurrent(alert, formConsultar);

                        //vai receber o historico da empresa nos ultimos 7 dias e guarda no recordStore
                        String historico = getHttpData("http://ichart.finance.yahoo.com/table.csv?a=0&b=1&c=2007&g=d&ignore=.csv&s=" + tick);
                        
                        
                        String[][] historicoParsed = new String[7][2];
                        historicoParsed = parseHistorico(historico, 7);

                        //Apaga todos os registos da empresa do recordStore
                        apagaRegistos(tick);
                        //Guarda 7 novos registos
                        for (int i = 0; i < 7; i++) {
                            insertRegisto(tick, historicoParsed[i][0], Double.parseDouble(historicoParsed[i][1]));
                        }
                        
                    }



                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                
//GEN-LINE:|7-commandAction|6|71-postAction

            }//GEN-BEGIN:|7-commandAction|7|80-preAction
        } else if (displayable == formHistorico) {
            if (command == mostrarCommand) {//GEN-END:|7-commandAction|7|80-preAction

                    if (isOnline) {
                        try {

                            datas = new String[7];
                            valores = new double[7];

                            //selecciona o tick name
                            tick = new String(choiceGroupEmpresas2.getString(choiceGroupEmpresas2.getSelectedIndex()));
                            
                            //verifica se o tick name é valido        
                            if(getHttpData("http://ichart.finance.yahoo.com/table.csv?a=10&b=5&c=2007&d=10&e=19&f=2007&g=d&ignore=.csv&s="+tick)==null)
                            {
                                Alert alert = new Alert("Erro", "Tick name desconhecido", null, AlertType.ERROR);
                                alert.setTimeout(3000);
                                Display display = Display.getDisplay(this);
                                display.setCurrent(alert, formHistorico);
                            }
                            else{
                                //vai receber o historico da empresa nos ultimos 7 dias e guarda no recordStore
                                String historico = getHttpData("http://ichart.finance.yahoo.com/table.csv?a=0&b=1&c=2007&g=d&ignore=.csv&s=" + tick);
                                String[][] historicoParsed = new String[7][2];
                                historicoParsed = parseHistorico(historico, 7);

                                //Apaga todos os registos da empresa do recordStore
                                apagaRegistos(tick);
                                //Guarda 7 novos registos
                                for (int i = 0; i < 7; i++) {
                                    insertRegisto(tick, historicoParsed[i][0], Double.parseDouble(historicoParsed[i][1]));
                                    datas[i] = historicoParsed[i][0];
                                    valores[i] = Double.parseDouble(historicoParsed[i][1]);
                                }
                                empresaGlobal = tick;
                                switchDisplayable(null, desenharGrafico());
                            }
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    } else {


                        //preencher array com 7 valores
                        int indice = choiceGroupEmpresas2.getSelectedIndex();
                        String empresa = choiceGroupEmpresas2.getString(indice);

                        byte[] linhaRecordBytes = null;

                        int j = 0;
                        datas = new String[7];
                        valores = new double[7];

                        try {


                            for (int i = 1; i <= recordStore.getNumRecords(); i++) {
                                try {
                                    linhaRecordBytes = recordStore.getRecord(i);
                                    String linhaRecord = new String(linhaRecordBytes);
                                    int indiceBarra = linhaRecord.indexOf("|");
                                    String nomeEmpresa = linhaRecord.substring(0, indiceBarra);

                                    if (empresa.compareTo(nomeEmpresa) == 0) {
                                        System.out.println("--------------------------------" + j);

                                        String strTemp = linhaRecord.substring(indiceBarra);
                                        strTemp = strTemp.substring(1);
                                        int indiceBarraFinal = strTemp.indexOf("|");
                                        String data = strTemp.substring(indiceBarra, indiceBarraFinal);

                                        strTemp = strTemp.substring(indiceBarraFinal);
                                        strTemp = strTemp.substring(1);

                                        double valor = Double.parseDouble(strTemp);

                                        datas[j] = data;
                                        valores[j] = valor;
                                        j++;
                                        if (j == 7) {
                                            break;
                                        }
                                    }
                                } catch (RecordStoreNotOpenException ex) {
                                    ex.printStackTrace();
                                } catch (InvalidRecordIDException ex) {
                                    ex.printStackTrace();
                                } catch (RecordStoreException ex) {
                                    ex.printStackTrace();
                                }
                            }
                        } catch (RecordStoreNotOpenException ex) {
                            ex.printStackTrace();
                        } catch (RecordStoreException ex) {
                            ex.printStackTrace();
                        }
                        empresaGlobal = empresa;
                        switchDisplayable(null, desenharGrafico());
                    }

                    

//GEN-LINE:|7-commandAction|8|80-postAction
            }else if (command == exitCommand4) {
                if(isOnline)
                    switchDisplayable(null, getFormOnline());
                else
                    switchDisplayable(null, getFormOffline());
            }
            else if (command == addTickCommand2) {
                switchDisplayable(null, getNovoTick2());
            }//GEN-BEGIN:|7-commandAction|9|19-preAction
        } else if (displayable == formOffline) {
            if (command == exitCommand) {//GEN-END:|7-commandAction|9|19-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|10|19-postAction
            // write post-action user code here
            } else if (command == itemHistorico1) {//GEN-LINE:|7-commandAction|11|36-preAction
                // write pre-action user code here
                boolean flag = false;
                if (!isOnline) {
                    String[] listaRecordStores = RecordStore.listRecordStores();

                    
                    int k = 0;
                    if (listaRecordStores != null) {
                        while (listaRecordStores[k] != null) {
                            if (listaRecordStores[k].compareTo("historico") == 0) {
                                flag = true;
                                break;
                            }
                            k++;
                        }
                    }
                }
                if(flag)
                    switchDisplayable(null, getFormHistorico());//GEN-LINE:|7-commandAction|12|36-postAction
               else {
                    Alert alert = new Alert("Erro", "Nao existem registos", null, AlertType.ERROR);
                    alert.setTimeout(4000);
                    Display display = Display.getDisplay(this);
                    display.setCurrent(alert, formOffline);
                }
                // write post-action user code here
            } else if (command == itemLigar) {//GEN-LINE:|7-commandAction|13|34-preAction
                // write pre-action user code here
                //verificar se ÃƒÂ© possÃƒÂ­vel ligar ao site do yahoo
                //se nÃƒÂ£o for, nÃƒÂ£o mostrar formOnline

                try {
                    HttpConnection conn = (HttpConnection) Connector.open("http://www.yahoo.com");
                    conn.setRequestMethod(HttpConnection.GET);
                    conn.setRequestProperty("user-agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
                    if (conn.getResponseCode() == HttpConnection.HTTP_OK) {
                        isOnline = true;
                        switchDisplayable(null, getFormOnline());//GEN-LINE:|7-commandAction|14|34-postAction
                    } else {
                        throw new IOException();
                    }

                    conn.close();
                } catch (IOException ex) {
                    ex.printStackTrace();

                    Alert alert = new Alert("Erro!", "Nao foi possivel ligar ao servidor", null, AlertType.ERROR);
                    alert.setTimeout(3000);
                    Display display = Display.getDisplay(this);
                    display.setCurrent(alert, formOffline);
                }
            }//GEN-BEGIN:|7-commandAction|15|42-preAction
        } else if (displayable == formOnline) {
            if (command == itemConsultar) {//GEN-END:|7-commandAction|15|42-preAction
                // write pre-action user code here
                switchDisplayable(null, getFormConsultar());//GEN-LINE:|7-commandAction|16|42-postAction
            // write post-action user code here
            } else if (command == itemDesligar) {//GEN-LINE:|7-commandAction|17|40-preAction
                // write pre-action user code here
                isOnline = false;
                switchDisplayable(null, getFormOffline());//GEN-LINE:|7-commandAction|18|40-postAction
            // write post-action user code here
            } else if (command == itemHistorico) {//GEN-LINE:|7-commandAction|19|44-preAction
                // write pre-action user code here
                switchDisplayable(null, getFormHistorico());//GEN-LINE:|7-commandAction|20|44-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|21|107-preAction
        } else if (displayable == novoTick) {
            if (command == exitCommand2) {//GEN-END:|7-commandAction|21|107-preAction
                // write pre-action user code here
                switchDisplayable(null, getFormConsultar());//GEN-LINE:|7-commandAction|22|107-postAction
            // write post-action user code here
            } else if (command == okCommand) {//GEN-LINE:|7-commandAction|23|111-preAction
                if (textnewTickName.getString().length() > 0) {
                    String str = textnewTickName.getString().toUpperCase();
                    choiceGroupEmpresas.append(str, null);
                }

                // write pre-action user code here
                switchDisplayable(null, getFormConsultar());//GEN-LINE:|7-commandAction|24|111-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|25|115-preAction
        } else if (displayable == novoTick2) {
            if (command == exitCommand3) {//GEN-END:|7-commandAction|25|115-preAction
                // write pre-action user code here
                switchDisplayable(null, getFormHistorico());//GEN-LINE:|7-commandAction|26|115-postAction
            // write post-action user code here
            } else if (command == okCommand1) {//GEN-LINE:|7-commandAction|27|117-preAction

                if (textNewTick2.getString().length() > 0) {
                    String str = textNewTick2.getString().toUpperCase();
                    choiceGroupEmpresas2.append(str, null);
                }

                switchDisplayable(null, getFormHistorico());//GEN-LINE:|7-commandAction|28|117-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|29|7-postCommandAction
        }//GEN-END:|7-commandAction|29|7-postCommandAction
        else if(displayable == grafico)
        {
            if(command == backCom)
            {
                switchDisplayable(null, getFormHistorico());
            }
        }


    //</editor-fold>                              

    }//GEN-BEGIN:|7-commandAction|30|
    //</editor-fold>//GEN-END:|7-commandAction|30|
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
        // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|18-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: formOffline ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of formOffline component.
     * @return the initialized component instance
     */
    public Form getFormOffline() {
        if (formOffline == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            formOffline = new Form("MarketGest", new Item[] { getStringItem() });//GEN-BEGIN:|14-getter|1|14-postInit
            formOffline.addCommand(getExitCommand());
            formOffline.addCommand(getItemLigar());
            formOffline.addCommand(getItemHistorico1());
            formOffline.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
        // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return formOffline;
    }
    //</editor-fold>//GEN-END:|14-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initiliazed instance of itemCommand component.
     * @return the initialized component instance
     */
    public Command getItemCommand() {
        if (itemCommand == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            itemCommand = new Command("Item", Command.ITEM, 0);//GEN-LINE:|22-getter|1|22-postInit
        // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return itemCommand;
    }
    //</editor-fold>//GEN-END:|22-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">//GEN-BEGIN:|24-getter|0|24-preInit
    /**
     * Returns an initiliazed instance of stringItem component.
     * @return the initialized component instance
     */
    public StringItem getStringItem() {
        if (stringItem == null) {//GEN-END:|24-getter|0|24-preInit
            // write pre-init user code here
            stringItem = new StringItem("Modo: ", "Offline");//GEN-BEGIN:|24-getter|1|24-postInit
            stringItem.setLayout(ImageItem.LAYOUT_DEFAULT);//GEN-END:|24-getter|1|24-postInit
        // write post-init user code here
        }//GEN-BEGIN:|24-getter|2|
        return stringItem;
    }
    //</editor-fold>//GEN-END:|24-getter|2|
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand1 ">//GEN-BEGIN:|27-getter|0|27-preInit
    /**
     * Returns an initiliazed instance of itemCommand1 component.
     * @return the initialized component instance
     */
    public Command getItemCommand1() {
        if (itemCommand1 == null) {//GEN-END:|27-getter|0|27-preInit
            // write pre-init user code here
            itemCommand1 = new Command("Ligar", Command.ITEM, 0);//GEN-LINE:|27-getter|1|27-postInit
        // write post-init user code here
        }//GEN-BEGIN:|27-getter|2|
        return itemCommand1;
    }
    //</editor-fold>//GEN-END:|27-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: screenCommand ">//GEN-BEGIN:|29-getter|0|29-preInit
    /**
     * Returns an initiliazed instance of screenCommand component.
     * @return the initialized component instance
     */
    public Command getScreenCommand() {
        if (screenCommand == null) {//GEN-END:|29-getter|0|29-preInit
            // write pre-init user code here
            screenCommand = new Command("Screen", Command.SCREEN, 0);//GEN-LINE:|29-getter|1|29-postInit
        // write post-init user code here
        }//GEN-BEGIN:|29-getter|2|
        return screenCommand;
    }
    //</editor-fold>//GEN-END:|29-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: screenCommand1 ">//GEN-BEGIN:|31-getter|0|31-preInit
    /**
     * Returns an initiliazed instance of screenCommand1 component.
     * @return the initialized component instance
     */
    public Command getScreenCommand1() {
        if (screenCommand1 == null) {//GEN-END:|31-getter|0|31-preInit
            // write pre-init user code here
            screenCommand1 = new Command("Screen", Command.SCREEN, 0);//GEN-LINE:|31-getter|1|31-postInit
        // write post-init user code here
        }//GEN-BEGIN:|31-getter|2|
        return screenCommand1;
    }
    //</editor-fold>//GEN-END:|31-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemLigar ">//GEN-BEGIN:|33-getter|0|33-preInit
    /**
     * Returns an initiliazed instance of itemLigar component.
     * @return the initialized component instance
     */
    public Command getItemLigar() {
        if (itemLigar == null) {//GEN-END:|33-getter|0|33-preInit
            // write pre-init user code heres
            itemLigar = new Command("Ligar", Command.ITEM, 0);//GEN-LINE:|33-getter|1|33-postInit
        // write post-init user code here
        }//GEN-BEGIN:|33-getter|2|
        return itemLigar;
    }
    //</editor-fold>//GEN-END:|33-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemHistorico1 ">//GEN-BEGIN:|35-getter|0|35-preInit
    /**
     * Returns an initiliazed instance of itemHistorico1 component.
     * @return the initialized component instance
     */
    public Command getItemHistorico1() {
        if (itemHistorico1 == null) {//GEN-END:|35-getter|0|35-preInit
            // write pre-init user code here
            itemHistorico1 = new Command("Historico", Command.ITEM, 0);//GEN-LINE:|35-getter|1|35-postInit
        // write post-init user code here
        }//GEN-BEGIN:|35-getter|2|
        return itemHistorico1;
    }
    //</editor-fold>//GEN-END:|35-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemDesligar ">//GEN-BEGIN:|39-getter|0|39-preInit
    /**
     * Returns an initiliazed instance of itemDesligar component.
     * @return the initialized component instance
     */
    public Command getItemDesligar() {
        if (itemDesligar == null) {//GEN-END:|39-getter|0|39-preInit
            // write pre-init user code here
            itemDesligar = new Command("Desligar", Command.ITEM, 0);//GEN-LINE:|39-getter|1|39-postInit
        // write post-init user code here
        }//GEN-BEGIN:|39-getter|2|
        return itemDesligar;
    }
    //</editor-fold>//GEN-END:|39-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemConsultar ">//GEN-BEGIN:|41-getter|0|41-preInit
    /**
     * Returns an initiliazed instance of itemConsultar component.
     * @return the initialized component instance
     */
    public Command getItemConsultar() {
        if (itemConsultar == null) {//GEN-END:|41-getter|0|41-preInit
            // write pre-init user code here
            itemConsultar = new Command("Consultar", Command.ITEM, 0);//GEN-LINE:|41-getter|1|41-postInit
        // write post-init user code here
        }//GEN-BEGIN:|41-getter|2|
        return itemConsultar;
    }
    //</editor-fold>//GEN-END:|41-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemHistorico ">//GEN-BEGIN:|43-getter|0|43-preInit
    /**
     * Returns an initiliazed instance of itemHistorico component.
     * @return the initialized component instance
     */
    public Command getItemHistorico() {
        if (itemHistorico == null) {//GEN-END:|43-getter|0|43-preInit
            // write pre-init user code here
            itemHistorico = new Command("Historico", Command.ITEM, 0);//GEN-LINE:|43-getter|1|43-postInit
        // write post-init user code here
        }//GEN-BEGIN:|43-getter|2|
        return itemHistorico;
    }
    //</editor-fold>//GEN-END:|43-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: formOnline ">//GEN-BEGIN:|37-getter|0|37-preInit
    /**
     * Returns an initiliazed instance of formOnline component.
     * @return the initialized component instance
     */
    public Form getFormOnline() {
        if (formOnline == null) {//GEN-END:|37-getter|0|37-preInit
            // write pre-init user code here
            formOnline = new Form("MarketGest", new Item[] { getStringItem1() });//GEN-BEGIN:|37-getter|1|37-postInit
            formOnline.addCommand(getItemDesligar());
            formOnline.addCommand(getItemConsultar());
            formOnline.addCommand(getItemHistorico());
            formOnline.setCommandListener(this);//GEN-END:|37-getter|1|37-postInit
        // write post-init user code here
        }//GEN-BEGIN:|37-getter|2|
        return formOnline;
    }
    //</editor-fold>//GEN-END:|37-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem1 ">//GEN-BEGIN:|38-getter|0|38-preInit
    /**
     * Returns an initiliazed instance of stringItem1 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem1() {
        if (stringItem1 == null) {//GEN-END:|38-getter|0|38-preInit
            // write pre-init user code here
            stringItem1 = new StringItem("Modo: ", "Online");//GEN-LINE:|38-getter|1|38-postInit
        // write post-init user code here
        }//GEN-BEGIN:|38-getter|2|
        return stringItem1;
    }
    //</editor-fold>//GEN-END:|38-getter|2|
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: formConsultar ">//GEN-BEGIN:|51-getter|0|51-preInit
    /**
     * Returns an initiliazed instance of formConsultar component.
     * @return the initialized component instance
     */
    public Form getFormConsultar() {
        if (formConsultar == null) {//GEN-END:|51-getter|0|51-preInit
            // write pre-init user code here
            formConsultar = new Form("Consulta na hora", new Item[] { getChoiceGroupEmpresas() });//GEN-BEGIN:|51-getter|1|51-postInit
            formConsultar.addCommand(getMostrarCommand2());
            formConsultar.addCommand(getExitCommand1());
            formConsultar.addCommand(getAddTickCommand());
            formConsultar.setCommandListener(this);//GEN-END:|51-getter|1|51-postInit
        // write post-init user code here
        }//GEN-BEGIN:|51-getter|2|
        return formConsultar;
    }
    //</editor-fold>//GEN-END:|51-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: screenCommand2 ">//GEN-BEGIN:|68-getter|0|68-preInit
    /**
     * Returns an initiliazed instance of screenCommand2 component.
     * @return the initialized component instance
     */
    public Command getScreenCommand2() {
        if (screenCommand2 == null) {//GEN-END:|68-getter|0|68-preInit
            // write pre-init user code here
            screenCommand2 = new Command("Screen", Command.SCREEN, 0);//GEN-LINE:|68-getter|1|68-postInit
        // write post-init user code here
        }//GEN-BEGIN:|68-getter|2|
        return screenCommand2;
    }
    //</editor-fold>//GEN-END:|68-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: mostrarCommand2 ">//GEN-BEGIN:|70-getter|0|70-preInit
    /**
     * Returns an initiliazed instance of mostrarCommand2 component.
     * @return the initialized component instance
     */
    public Command getMostrarCommand2() {
        if (mostrarCommand2 == null) {//GEN-END:|70-getter|0|70-preInit
            // write pre-init user code here
            mostrarCommand2 = new Command("Mostrar", Command.OK, 0);//GEN-LINE:|70-getter|1|70-postInit
        // write post-init user code here
        }//GEN-BEGIN:|70-getter|2|
        return mostrarCommand2;
    }
    //</editor-fold>//GEN-END:|70-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: mostrarCommand ">//GEN-BEGIN:|79-getter|0|79-preInit
    /**
     * Returns an initiliazed instance of mostrarCommand component.
     * @return the initialized component instance
     */
    public Command getMostrarCommand() {
        if (mostrarCommand == null) {//GEN-END:|79-getter|0|79-preInit
            // write pre-init user code here
            mostrarCommand = new Command("Mostrar", Command.OK, 0);//GEN-LINE:|79-getter|1|79-postInit
        // write post-init user code here   
        }//GEN-BEGIN:|79-getter|2|
        return mostrarCommand;
    }
    //</editor-fold>//GEN-END:|79-getter|2|
    private MyCanvas desenharGrafico() {
        
        grafico = new MyCanvas(empresaGlobal, datas, valores);
        grafico.addCommand(getBackCom());
        grafico.setCommandListener(this);

        return grafico;
    }
    public Command getBackCom()
    {
        if(backCom==null)
            backCom = new Command("Exit", Command.BACK,0);
        return backCom;
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: choiceGroupEmpresas ">//GEN-BEGIN:|62-getter|0|62-preInit
    /**
     * Returns an initiliazed instance of choiceGroupEmpresas component.
     * @return the initialized component instance
     */
    public ChoiceGroup getChoiceGroupEmpresas() {
        if (choiceGroupEmpresas == null) {//GEN-END:|62-getter|0|62-preInit
            // write pre-init user code here
            choiceGroupEmpresas = new ChoiceGroup("Empresas", Choice.POPUP);//GEN-BEGIN:|62-getter|1|62-postInit
            choiceGroupEmpresas.append("IBM", null);
            choiceGroupEmpresas.append("MSFT", null);
            choiceGroupEmpresas.append("DELL", null);
            choiceGroupEmpresas.append("CSCO", null);
            choiceGroupEmpresas.append("AMZN", null);
            choiceGroupEmpresas.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
            choiceGroupEmpresas.setSelectedFlags(new boolean[] { false, false, false, false, false });
            choiceGroupEmpresas.setFont(0, null);
            choiceGroupEmpresas.setFont(1, null);
            choiceGroupEmpresas.setFont(2, null);
            choiceGroupEmpresas.setFont(3, null);
            choiceGroupEmpresas.setFont(4, null);//GEN-END:|62-getter|1|62-postInit
        // write post-init user code here
        }//GEN-BEGIN:|62-getter|2|
        return choiceGroupEmpresas;
    }
    //</editor-fold>//GEN-END:|62-getter|2|
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: formHistorico ">//GEN-BEGIN:|59-getter|0|59-preInit
    /**
     * Returns an initiliazed instance of formHistorico component.
     * @return the initialized component instance
     */
    public Form getFormHistorico() {
        if (formHistorico == null) {//GEN-END:|59-getter|0|59-preInit
 
        }


        byte[] linhaRecordBytes = null;
        String[] listaEmpresas = new String[128];



        //verificar se esta online ou offline
        //se estiver offline pesquisar todos os ficheiros e mete-los na lista de elementos
        if (!isOnline) {
            try {


                choiceGroupEmpresas2 = new ChoiceGroup("Empresas", Choice.POPUP);


                recordStore = RecordStore.openRecordStore("historico", false);

                for (int i = 1; i <= recordStore.getNumRecords(); i++) {
                    linhaRecordBytes = recordStore.getRecord(i);
                    String linhaRecord = new String(linhaRecordBytes);
                    int indice = linhaRecord.indexOf("|");
                    String nomeEmpresa = linhaRecord.substring(0, indice);


                    boolean flagExiste = false;
                    //verificar se a empresa ja foi identificada
                    int j = 0;
                    while (listaEmpresas[j] != null) {
                        if (listaEmpresas[j].compareTo(nomeEmpresa) == 0) {
                            flagExiste = true;
                            break;
                        }
                        j++;
                    }

                    //mete empresa no array de empresas e no choicegroup
                    if (!flagExiste) {
                        listaEmpresas[j] = nomeEmpresa;
                        choiceGroupEmpresas2.append(nomeEmpresa, null);
                    }

                }
                choiceGroupEmpresas2.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
                formHistorico = new Form("Consulta Historico", new Item[]{getChoiceGroupEmpresas2()});
                formHistorico.addCommand(getMostrarCommand());
                formHistorico.addCommand(getExitCommand4());
                formHistorico.setCommandListener(this);


            //choiceGroupEmpresas2.setFont(0, null);
            } catch (InvalidRecordIDException ex) {
                ex.printStackTrace();
            } catch (RecordStoreNotOpenException ex) {
                ex.printStackTrace();
            } catch (RecordStoreException ex) {
                ex.printStackTrace();

            }

        }
        if (isOnline) {
            choiceGroupEmpresas2 = new ChoiceGroup("Empresas", Choice.POPUP);
            choiceGroupEmpresas2.append("IBM", null);
            choiceGroupEmpresas2.append("MSFT", null);
            choiceGroupEmpresas2.append("DELL", null);
            choiceGroupEmpresas2.append("CSCO", null);
            choiceGroupEmpresas2.append("AMZN", null);

            choiceGroupEmpresas2.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
            choiceGroupEmpresas2.setSelectedFlags(new boolean[]{false, false, false, false, false, false});
            formHistorico = new Form("Consulta Historico", new Item[]{getChoiceGroupEmpresas2()});
            formHistorico.addCommand(getMostrarCommand());
            formHistorico.addCommand(getExitCommand4());
            formHistorico.addCommand(getAddTickCommand2());
            formHistorico.setCommandListener(this);

        }

            /*
formHistorico = new Form ("Consulta Historico", new Item[] { getChoiceGroupEmpresas2 () });//GEN-BEGIN:|59-getter|1|59-postInit
formHistorico.addCommand (getMostrarCommand ());
formHistorico.setCommandListener (this);//GEN-END:|59-getter|1|59-postInit
            */

        return formHistorico;
    }
    /*
            
}//GEN-BEGIN:|59-getter|2|
return formHistorico;
}
//</editor-fold>//GEN-END:|59-getter|2|
*/
    public Command getExitCommand4() {
        if (exitCommand4 == null) {                                 
            // write pre-init user code here
            exitCommand4 = new Command("Exit", Command.EXIT, 0);                                   
            // write post-init user code here
        }                         
        return exitCommand4;
    }
    
     public Command getAddTickCommand2() {
        if (addTickCommand2 == null) {
            // write pre-init user code here
            addTickCommand2 = new Command("Novo Tick", Command.ITEM, 0);
            // write post-init user code here
        }
        return addTickCommand2;
    }
    
    //Esta funÃƒÂ§ÃƒÂ£o controi UMA string com os dados de UMA empresa e insere no recordStore
    //Ainda nao foi testada
    public void insertRegisto(String nome, String data, double valor)
    {
               
        //cria um recordStore onde serÃƒÂ£o armazenados os dados
        try {
            System.out.println("creating data base");
            recordStore = RecordStore.openRecordStore("historico", true);
        } catch (RecordStoreException rse) {
            System.err.println(rse.toString());
        }
        
        //constroi a string que serÃƒÂ¡ inserida no recordStore
        String registo = "";
        registo += nome ;
        registo += "|";
        registo += data.toString();
        registo += "|";
        registo += valor;
        
     
        
        try
            {
                recordID = recordStore.addRecord(registo.getBytes(), 0, registo.getBytes().length);
            }
            catch(RecordStoreException rse)
            {
                System.err.println(rse.toString());
            }
    }
    
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: choiceGroupEmpresas2 ">//GEN-BEGIN:|75-getter|0|75-preInit
    /**
     * Returns an initiliazed instance of choiceGroupEmpresas2 component.
     * @return the initialized component instance
     */
    public ChoiceGroup getChoiceGroupEmpresas2() {
        if (choiceGroupEmpresas2 == null) {//GEN-END:|75-getter|0|75-preInit
            // write pre-init user code here
            choiceGroupEmpresas2 = new ChoiceGroup("Empresas", Choice.POPUP);//GEN-BEGIN:|75-getter|1|75-postInit
            choiceGroupEmpresas2.setSelectedFlags(new boolean[] {  });//GEN-END:|75-getter|1|75-postInit
            // write post-init user code here
        }//GEN-BEGIN:|75-getter|2|
        return choiceGroupEmpresas2;
    }
    //</editor-fold>//GEN-END:|75-getter|2|









    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand1 ">//GEN-BEGIN:|99-getter|0|99-preInit
    /**
     * Returns an initiliazed instance of exitCommand1 component.
     * @return the initialized component instance
     */
    public Command getExitCommand1() {
        if (exitCommand1 == null) {//GEN-END:|99-getter|0|99-preInit
            // write pre-init user code here
            exitCommand1 = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|99-getter|1|99-postInit
            // write post-init user code here
        }//GEN-BEGIN:|99-getter|2|
        return exitCommand1;
    }
    //</editor-fold>//GEN-END:|99-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: addTickCommand ">//GEN-BEGIN:|102-getter|0|102-preInit
    /**
     * Returns an initiliazed instance of addTickCommand component.
     * @return the initialized component instance
     */
    public Command getAddTickCommand() {
        if (addTickCommand == null) {//GEN-END:|102-getter|0|102-preInit
            // write pre-init user code here
            addTickCommand = new Command("Novo Tick", Command.ITEM, 0);//GEN-LINE:|102-getter|1|102-postInit
            // write post-init user code here
        }//GEN-BEGIN:|102-getter|2|
        return addTickCommand;
    }
    //</editor-fold>//GEN-END:|102-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand2 ">//GEN-BEGIN:|106-getter|0|106-preInit
    /**
     * Returns an initiliazed instance of exitCommand2 component.
     * @return the initialized component instance
     */
    public Command getExitCommand2() {
        if (exitCommand2 == null) {//GEN-END:|106-getter|0|106-preInit
            // write pre-init user code here
            exitCommand2 = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|106-getter|1|106-postInit
            // write post-init user code here
        }//GEN-BEGIN:|106-getter|2|
        return exitCommand2;
    }
    //</editor-fold>//GEN-END:|106-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: novoTick ">//GEN-BEGIN:|104-getter|0|104-preInit
    /**
     * Returns an initiliazed instance of novoTick component.
     * @return the initialized component instance
     */
    public Form getNovoTick() {
        if (novoTick == null) {//GEN-END:|104-getter|0|104-preInit
            // write pre-init user code here
            novoTick = new Form("Novo Tick", new Item[] { getTextnewTickName() });//GEN-BEGIN:|104-getter|1|104-postInit
            novoTick.addCommand(getExitCommand2());
            novoTick.addCommand(getOkCommand());
            novoTick.setCommandListener(this);//GEN-END:|104-getter|1|104-postInit
            // write post-init user code here
        }//GEN-BEGIN:|104-getter|2|
        return novoTick;
    }
    //</editor-fold>//GEN-END:|104-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textnewTickName ">//GEN-BEGIN:|105-getter|0|105-preInit
    /**
     * Returns an initiliazed instance of textnewTickName component.
     * @return the initialized component instance
     */
    public TextField getTextnewTickName() {
        if (textnewTickName == null) {//GEN-END:|105-getter|0|105-preInit
            // write pre-init user code here
            textnewTickName = new TextField("Insira Tick Name", null, 32, TextField.ANY);//GEN-LINE:|105-getter|1|105-postInit
            // write post-init user code here
        }//GEN-BEGIN:|105-getter|2|
        return textnewTickName;
    }
    //</editor-fold>//GEN-END:|105-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|110-getter|0|110-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|110-getter|0|110-preInit
            // write pre-init user code here
            okCommand = new Command("Ok", Command.OK, 0);//GEN-LINE:|110-getter|1|110-postInit
            // write post-init user code here
        }//GEN-BEGIN:|110-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|110-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: novoTick2 ">//GEN-BEGIN:|113-getter|0|113-preInit
    /**
     * Returns an initiliazed instance of novoTick2 component.
     * @return the initialized component instance
     */
    public Form getNovoTick2() {
        if (novoTick2 == null) {//GEN-END:|113-getter|0|113-preInit
            // write pre-init user code here
            novoTick2 = new Form("Novo Tick", new Item[] { getTextNewTick2() });//GEN-BEGIN:|113-getter|1|113-postInit
            novoTick2.addCommand(getExitCommand3());
            novoTick2.addCommand(getOkCommand1());
            novoTick2.setCommandListener(this);//GEN-END:|113-getter|1|113-postInit
            // write post-init user code here
        }//GEN-BEGIN:|113-getter|2|
        return novoTick2;
    }
    //</editor-fold>//GEN-END:|113-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand3 ">//GEN-BEGIN:|114-getter|0|114-preInit
    /**
     * Returns an initiliazed instance of exitCommand3 component.
     * @return the initialized component instance
     */
    public Command getExitCommand3() {
        if (exitCommand3 == null) {//GEN-END:|114-getter|0|114-preInit
            // write pre-init user code here
            exitCommand3 = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|114-getter|1|114-postInit
            // write post-init user code here
        }//GEN-BEGIN:|114-getter|2|
        return exitCommand3;
    }
    //</editor-fold>//GEN-END:|114-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand1 ">//GEN-BEGIN:|116-getter|0|116-preInit
    /**
     * Returns an initiliazed instance of okCommand1 component.
     * @return the initialized component instance
     */
    public Command getOkCommand1() {
        if (okCommand1 == null) {//GEN-END:|116-getter|0|116-preInit
            // write pre-init user code here
            okCommand1 = new Command("Ok", Command.OK, 0);//GEN-LINE:|116-getter|1|116-postInit
            // write post-init user code here
        }//GEN-BEGIN:|116-getter|2|
        return okCommand1;
    }
    //</editor-fold>//GEN-END:|116-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textNewTick2 ">//GEN-BEGIN:|118-getter|0|118-preInit
    /**
     * Returns an initiliazed instance of textNewTick2 component.
     * @return the initialized component instance
     */
    public TextField getTextNewTick2() {
        if (textNewTick2 == null) {//GEN-END:|118-getter|0|118-preInit
            // write pre-init user code here
            textNewTick2 = new TextField("Insira novo tick", null, 32, TextField.ANY);//GEN-LINE:|118-getter|1|118-postInit
            // write post-init user code here
        }//GEN-BEGIN:|118-getter|2|
        return textNewTick2;
    }
    //</editor-fold>//GEN-END:|118-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textBox ">//GEN-BEGIN:|119-getter|0|119-preInit
    /**
     * Returns an initiliazed instance of textBox component.
     * @return the initialized component instance
     */
    public TextBox getTextBox() {
        if (textBox == null) {//GEN-END:|119-getter|0|119-preInit
            // write pre-init user code here
            textBox = new TextBox("textBox", null, 100, TextField.ANY);//GEN-LINE:|119-getter|1|119-postInit
            // write post-init user code here
        }//GEN-BEGIN:|119-getter|2|
        return textBox;
    }
    //</editor-fold>//GEN-END:|119-getter|2|





    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

}

